from os import error
from flask import Blueprint,render_template, request,session,redirect
from config import conn,cur,mutex ,r as redis
import datetime

Appointment = Blueprint('Appointment',__name__)
 
@Appointment.route('/',methods=["GET", "POST"])
def index():
    return render_template('/appointment/index.html')

@Appointment.before_request
def before():
    if session.get('id') is None or session.get('role') != 'patient':
        return redirect('/Login/',code=301)
    else:
        pass

@Appointment.route('/getappint',methods=["GET", "POST"])
def getappint():
    if request.method == "POST":
        patient_id = session.get('id')
        date = request.form.get("date")


    try:
        mutex.acquire()
        # get doctor id
        sqlstr = "SELECT doctor_id from personal_info where id = {};".format(patient_id)
        print(sqlstr)
        cur.execute(sqlstr)
        doctor_id = cur.fetchone()[0]
        # get appointment
        # sqlstr="insert into  personal_info  (name,password,gender,age,weight,height,doctor_name,doc_visit_date,annual_checkup) VALUES ({},{},{},{},{},{},{},{},{})".format(username,password,gender,age,weight,height,doc_name,doc_visit_date,annual_checkup)
        sqlstr="select part from appointment where doctor_id = {} and date = '{}';".format(doctor_id,date)
        # sqlstr="select part from appointment where doctor_id = {} and patient_id = {} and date = '{}';".format(doctor_id,patient_id,date)
        print(sqlstr)
        
        cur.execute(sqlstr)
        res = [t[0] for t in cur.fetchall()]
        mutex.release()
        print(res)

    #   conn.commit()

        return {'message':'success','res':res}
    except Exception as re:
        print(re)
        return {'message':'error_registration'}

@Appointment.route('/getpatientappint',methods=["GET", "POST"])
def getpatientappint():
    if request.method == "POST":
        patient_id = session.get('id')
        date = request.form.get("date")


    try:
        # get doctor id
        sqlstr = "SELECT doctor_id from personal_info where id = {};".format(patient_id)
        cur.execute(sqlstr)
        doctor_id = cur.fetchone()[0]
        # get patient appointment
        # sqlstr="insert into  personal_info  (name,password,gender,age,weight,height,doctor_name,doc_visit_date,annual_checkup) VALUES ({},{},{},{},{},{},{},{},{})".format(username,password,gender,age,weight,height,doc_name,doc_visit_date,annual_checkup)
        # sqlstr="select part from appointment where doctor_id = {} and date = '{}';".format(doctor_id,date)
        sqlstr="select part from appointment where doctor_id = {} and patient_id = {} and date = '{}';".format(doctor_id,patient_id,date)
        print(sqlstr)
        mutex.acquire()
        cur.execute(sqlstr)
        mutex.release()

        res = [t[0] for t in cur.fetchall()]
        print(res)

    #   conn.commit()

        return {'message':'success','res':res}
    except Exception as re:
        print(re)
        return {'message':'error_registration'}

@Appointment.route('/getpatientAppointrecord',methods=["GET", "POST"])
def getpatientAppointrecord():
    if request.method == "POST":
        patient_id = session.get('id')

    try:
        
        # get patient Appointrecord
        # sqlstr="insert into  personal_info  (name,password,gender,age,weight,height,doctor_name,doc_visit_date,annual_checkup) VALUES ({},{},{},{},{},{},{},{},{})".format(username,password,gender,age,weight,height,doc_name,doc_visit_date,annual_checkup)
        # sqlstr="select part from appointment where doctor_id = {} and date = '{}';".format(doctor_id,date)
        sqlstr="select date, part, name from appointment,doctor_info where appointment.doctor_id = doctor_info.id and patient_id = {} ORDER BY date DESC, part;".format(patient_id)
        print(sqlstr)
        mutex.acquire()
        cur.execute(sqlstr)
        res = cur.fetchall()
        mutex.release()
        print(res)

    #   conn.commit()

        return {'message':'success','res':res}
    except Exception as re:
        print(re)
        return {'message':'error_getpatientAppointrecord'}

@Appointment.route('/makeappoint',methods=["GET", "POST"])
def makeappoint():
    if request.method == "POST":
        patient_id = session.get('id')
        date = request.form.get("date")
        part = request.form.get("part")

    try:
        print('id: ',id)
        print('date: ',date)
        print('part: ',part)
        # get doctor id
        sqlstr = "SELECT doctor_id from personal_info where id = {};".format(patient_id)
        cur.execute(sqlstr)
        doctor_id = cur.fetchone()[0]
        # get appointment
        # sqlstr="insert into  personal_info  (name,password,gender,age,weight,height,doctor_name,doc_visit_date,annual_checkup) VALUES ({},{},{},{},{},{},{},{},{})".format(username,password,gender,age,weight,height,doc_name,doc_visit_date,annual_checkup)
        sqlstr="insert into appointment (doctor_id,patient_id,date,part) VALUES ({},{},'{}',{});".format(doctor_id,patient_id,date,part)
        print(sqlstr)
        mutex.acquire()
        cur.execute(sqlstr)
        conn.commit()
        mutex.release()

        return {'message':'success'}
    except Exception as re:
        print(re)
        return {'message':'error_registration'}